<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="unset" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <h3 class="title">重置用户密码</h3>
      <el-form-item prop="name">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="unset.name" name="name" type="text" auto-complete="on" placeholder="登录名" />
      </el-form-item>
      <el-form-item prop="certificate">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="unset.certificate"
          type="text"
          name="certificate"
          auto-complete="on"
          placeholder="重置密码凭证" />
      </el-form-item>
      <el-form-item prop="oneCode" class="one-code">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="unset.oneCode"
          type="text"
          name="code"
          auto-complete="on"
          placeholder="一次性登录码"
          @keyup.enter.native="unsetLogin" />
      </el-form-item>
      <a class="resetPassword" @click.prevent="ApplyUnset"> 请申诉获得一次性登陆码</a>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click=" handleLogin">
          登录
        </el-button>
      </el-form-item>
      <a class="login" href="/login"> 想起密码？立即登录</a>
    </el-form>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import Admin from '@/api/admin'
export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validateCertificate = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('凭证长度不能小于5位'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length !== 35) {
        callback(new Error('一次性登录码无效'))
      } else {
        callback()
      }
    }
    return {
      unset: {
        name: '',
        certificate: '',
        oneCode: ''
      },
      codeRule: [{ required: false, trigger: 'blur', validator: validateCode }],
      loginRules: {
        name: [{ required: true, trigger: 'blur', validator: validateUsername }],
        certificate: [{ required: true, trigger: 'blur', validator: validateCertificate }]
      },
      loading: false
    }
  },
  methods: {
    ApplyUnset() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          Admin.applyUnsetPassword(this.unset).then(response => {
            // 提示信息
            this.loading = false
            this.$alert('请保存好重置密码凭证<br>管理员会尽快处理，并联系您提供一次性登录码<br>获得一次性登录码后，请使用对应凭证及时登录并修改新密码', '申请成功', {
              dangerouslyUseHTMLString: true,
              confirmButtonText: '确定'
            })
            this.$router.push({ path: '/login' })
          }).catch(() => {
            this.loading = false
          })
          this.loading = false
        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('unsetLogin', this.unset)
            .then(() => {
              this.loading = false
              this.$router.push({ path: '/user/info' })
            }).catch(error => {
              this.loading = false
            })
          this.loading = false
        } else {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .one-code{
    margin-bottom: 5px;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  form{
    display: flex;
    flex-direction: column;
  }
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  a.login{
    color: #fff;
    display:block;
    text-align: center;
    margin-bottom: 10px;
  }
  a.login:hover{
    color: rgb(167, 151, 151);
  }
  a.resetPassword{
    color: #fff;
    text-align:center;
    margin: 5px 0;
    display: block;
  }
  a.resetPassword:hover{
    color: rgb(167, 151, 151);
  }
}
</style>
